package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CarDetails;
import com.jzg.jcptadmin.data.vo.FastOnlineData;
import com.jzg.jcptadmin.data.vo.OfflineDetetionDataSearchByVin;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.OnLineSearchByVINInterface;
import com.jzg.jcptadmin.viewinterface.SearchByVINInterface;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * KuaiSuOnLinePresenter
 *
 * @author sunbl
 *         created at 2015/12/31 11:15
 */
public class OnLineSearchByVINPresenter extends BasePresenter<OnLineSearchByVINInterface> {
    private final DataManager mDataManager;
    private OnLineSearchByVINInterface mvpView;
    private Subscription mSubscription;
    private int pageIndex = 1;

    public OnLineSearchByVINPresenter(DataManager instance) {
        super();
        mDataManager = instance;
    }

    public void resetPageIndex(){
        pageIndex = 1;
    }

    /***
     * 认领任务和认领审核
     * @param userid
     * @param taskid
     * @param key
     * @param value
     * @param op  MessageOperation 值为-1的时候是抢单
     */
    public void claimTask(String userid, final String taskid, String key, String value, final int op){
        checkViewAttached();
        if (AppContext.isNetWork){
            Map<String,String> params = new HashMap<>();
            params.put(key, value);
            params.put("taskid", taskid);
            params.put("userId", userid);
            if(op==-1){
                params.put("qiangdan", "1");
            }
            mSubscription = mDataManager.claimTask(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<SuccessData>() {
                @Override
                public void onCompleted() {
                    mvpView.dismissDialog();
                }
                @Override
                public void onError(Throwable e) {
                    if (e != null&&mvpView!=null) {
                        mvpView.showError(Constant.errr_net);
                    }
                }

                @Override
                public void onNext(SuccessData successData) {
                    if(successData.isSucceed()){
                        mvpView.claimSucceed();
                    }else{
                        String qiangdan = successData.getQiangdan();
                        if("1".equals(qiangdan)){
                            mvpView.qiangdan(taskid,successData.getMsg());
                        }else{
                            mvpView.claimFailed(successData.getMsg());
                        }
                    }
                }
            });
        }
    }

    /***
     * 线下认领审核
     * @param userid
     * @param taskid
     * @param key
     * @param value
     */
    public void offLineclaimTask(String userid,final String taskid,String key,String value,final int op){
        checkViewAttached();
        if (AppContext.isNetWork){
            Map<String,String> params = new HashMap<>();
            params.put(key, value);
            params.put("taskid", taskid);
            params.put("userId", userid);
            if(op==-1){
                params.put("qiangdan", "1");
            }
            mSubscription = mDataManager.claimTask(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<SuccessData>() {
                @Override
                public void onCompleted() {
                    mvpView.dismissDialog();
                }
                @Override
                public void onError(Throwable e) {
                    if (e != null&&mvpView!=null) {
                        mvpView.showError(e.getMessage());
                    } else {
                        mvpView.showError(Constant.errr_net);
                    }
                }

                @Override
                public void onNext(SuccessData successData) {
                    if(successData.isSucceed()){
                        mvpView.claimSucceed();
                    }else{
                        String qiangdan = successData.getQiangdan();
                        if("1".equals(qiangdan)){
                            mvpView.qiangdan(taskid,successData.getMsg());
                        }else{
                            mvpView.claimFailed(successData.getMsg());
                        }
                    }
                }
            });
        }
    }


    /***
     * 认领审核
     * @param userid
     * @param taskid
     * @param key
     * @param value
     */
    public void claimCheckTask(String userid,String taskid,String key,String value){
        checkViewAttached();
        if (AppContext.isNetWork){
            Map<String,String> params = new HashMap<>();
            params.put(key, value);
            params.put("taskid", taskid);
            params.put("userId", userid);
            mSubscription = mDataManager.claimTask(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<SuccessData>() {
                @Override
                public void onCompleted() {
                    mvpView.dismissDialog();
                }
                @Override
                public void onError(Throwable e) {
                    if (e != null&&mvpView!=null) {
                        mvpView.showError(Constant.errr_net);
                    }
                }

                @Override
                public void onNext(SuccessData successData) {
                    if(successData.isSucceed()){
                        mvpView.claimSucceed();
                    }else{
                        mvpView.claimFailed(successData.getMsg());
                    }
                }
            });
        }
    }



    /**
     * 加载数据和分页加载更多通用
     */
    public void loadDataNew(String userID, String datetype, String status, String Operation,String PageId,String isShowAll,String vin) {
        checkViewAttached();
        if (AppContext.isNetWork)
            mSubscription = mDataManager.loadFastOnlineDataNew(pageIndex,userID, datetype, status, Operation, PageId,isShowAll,vin)
                    .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<FastOnlineData>() {
                        @Override
                        public void onCompleted() {
                            mvpView.dismissDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (e != null&&mvpView!=null) {
                                mvpView.showError(Constant.errr_net);
                            }
                        }

                        @Override
                        public void onNext(FastOnlineData fastOnlineData) {
                            if(pageIndex==1){
                                mvpView.showData(fastOnlineData);
                            }else{
                                mvpView.showMoreData(fastOnlineData);
                            }
                            List<FastOnlineData.AppraiseListEntity> appraiseList = fastOnlineData.getAppraiseList();
                            if(appraiseList!=null && appraiseList.size()>0){
                                pageIndex++;
                            }
                        }
                    });
    }




    /**
     * 加载数据
     */
    public void loadData(String userID, String datetype, String status, String Operation,String PageId,String isShowAll,String vin) {
        checkViewAttached();
        if (AppContext.isNetWork)
            mSubscription = mDataManager.loadFastOnlineData(userID, datetype, status, Operation, PageId,isShowAll,vin)
                    .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<FastOnlineData>() {
                @Override
                public void onCompleted() {
                    mvpView.dismissDialog();
                }

                @Override
                public void onError(Throwable e) {
                    if (e != null&&mvpView!=null) {
                        mvpView.showError(Constant.errr_net);
                    }
                }

                @Override
                public void onNext(FastOnlineData fastOnlineData) {
                    mvpView.showData(fastOnlineData);
                }
            });

    }

    /**
     * 加载更多
     */
    public void loadMoreData(String userID, String datetype, String status, String Operation,
                             String PageId,String isShowAll) {
        checkViewAttached();
        mSubscription = mDataManager.loadFastOnlineData(userID, datetype, status, Operation, PageId,isShowAll,"")
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<FastOnlineData>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        if (e != null&&mvpView!=null) {
                            mvpView.showError("加载数据失败！");
                        }
                    }
                    @Override
                    public void onNext(FastOnlineData fastOnlineData) {
                        mvpView.showMoreData(fastOnlineData);
                    }
                });

    }

    @Override
    public void attachView(OnLineSearchByVINInterface mvpView) {
        super.attachView(mvpView);
        this.mvpView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null)
            mSubscription.unsubscribe();

    }

    /**
     *
     * @param userID
     * @param taskId
     * @param type
     * @param source 调用接口的标识、
     * 1、调用从快速线上评估接口
     * 2、调用从专业线下接口
     */
    public void loadDetail(String userID, String taskId, final String type,int source) {
        if (1==source){
        mSubscription = mDataManager.loadFastOnlineDetailData(userID, taskId)
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<CarDetails>() {
                    @Override
                    public void onCompleted() { }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null&&mvpView!=null) {
                            mvpView.showError(e.toString());
                        }
                    }

                    @Override
                    public void onNext(CarDetails carDetails) {
                        if(carDetails.getStatus().equals("100")){
                            mvpView.onOnLineDetailSuc(carDetails,type);
                        }else{
                            mvpView.dataOnLineFailed(carDetails.getMsg());
                        }

                    }
                });
    }else{
//            mSubscription = mDataManager.loadFastOnlineDetailData(userID, taskId)
//                    .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<CarDetails>() {
//                        @Override
//                        public void onCompleted() { }
//
//                        @Override
//                        public void onError(Throwable e) {
//                            if (e != null&&mvpView!=null) {
//                                mvpView.showError(e.toString());
//                            }
//                        }
//
//                        @Override
//                        public void onNext(CarDetails carDetails) {
//                            if(carDetails.getStatus().equals("100")){
//                                mvpView.onOnLineDetailSuc(carDetails,type);
//                            }else{
//                                mvpView.dataOnLineFailed(carDetails.getMsg());
//                            }
//
//                        }
//                    });
        }
    }

    /**
     * 通过vin查询线上数据
     */
    public void loadOnLineSearchByVinData(String pageId){
        Map<String, String> params = mvpView.getOnLineSearchByVinParameters(pageId);
        if (params == null) {
                return;
        }
        mSubscription = mDataManager.loadFastOnlineSearchByVinDataNew(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<FastOnlineData>() {
                    @Override
                    public void onCompleted() {
                        mvpView.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null && mvpView != null) {
                            mvpView.onOnLineAndOffLineLoadDataError(e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(FastOnlineData fastOnlineData) {

                        if(pageIndex==1){
                            mvpView.onOnLineLoadDataSuccess(fastOnlineData);
                        }else{
                            mvpView.showOnLineMoreData(fastOnlineData);
                        }
                        List<FastOnlineData.AppraiseListEntity> appraiseList = fastOnlineData.getAppraiseList();
                        if(appraiseList!=null && appraiseList.size()>0){
                            pageIndex++;
                        }
                    }
                });
    }

    /**
     * 通过vin查询数据
     * @param source 调用接口的标识、
     * 1、调用从快速线上评估接口
     * 2、调用从专业线下接口
     */
    public void loadSearchByVinData(String userID, String datetype, String status, String Operation,String PageId,String isShowAll,String vin,int source){

//        checkViewAttached();
//
//
//        if (1==source){
////            Map<String, String> evaluationParameters = mvpView.getSearchByVinParameters();
////            if (evaluationParameters == null) {
////                return;
////            }
//            mSubscription = mDataManager.loadFastOnlineDataNew(pageIndex,userID, datetype, status, Operation, PageId,isShowAll,vin)
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribeOn(Schedulers.io())
//                    .subscribe(new Subscriber<FastOnlineData>() {
//                        @Override
//                        public void onCompleted() {
//                            mvpView.dismissDialog();
//                        }
//
//                        @Override
//                        public void onError(Throwable e) {
//                            if (e != null && mvpView != null) {
//                                mvpView.onOnLineAndOffLineLoadDataError(e.getMessage());
//                            }
//                        }
//
//                        @Override
//                        public void onNext(FastOnlineData fastOnlineData) {
//
//                            if(pageIndex==1){
//                                mvpView.onOnLineLoadDataSuccess(fastOnlineData);
//                            }else{
//                                mvpView.showOnLineMoreData(fastOnlineData);
//                            }
//                            List<FastOnlineData.AppraiseListEntity> appraiseList = fastOnlineData.getAppraiseList();
//                            if(appraiseList!=null && appraiseList.size()>0){
//                                pageIndex++;
//                            }
//                        }
//                    });
//        }else if (2==source){
//            mSubscription = mDataManager.loadOfflineDetectionNew(pageIndex,userID,datetype,status,Operation,PageId)
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribeOn(Schedulers.io())
//                    .subscribe(new Observer<OfflineDetectionData>() {
//                @Override
//                public void onCompleted() { }
//                @Override
//                public void onError(Throwable e) {
//                    if(e!=null){
//                        mView.showError(e.getMessage());
//                    }
//                }
//                @Override
//                public void onNext(OfflineDetectionData offlineDetectionData) {
//                    Logger.e(offlineDetectionData.toString());
//                    if(offlineDetectionData.getStatus()==100){
//                        if(pageIndex==1){
//                            mView.showDatas(offlineDetectionData);
//                        }else{
//                            mView.showMoreDatas(offlineDetectionData);
//                        }
//
//                    }else{
//                        mView.showError(offlineDetectionData.getMsg());
//                    }
//                    List<OfflineDetectionData.OffLineTaskListEntity> offLineTaskList = offlineDetectionData.getOffLineTaskList();
////                for (OfflineDetectionData.OffLineTaskListEntity item:offLineTaskList  ) {
////                    Log.e("pk","message operation:"+item.getMessageOperation()+",chejiaohao:"+item.getVin());
////                }
//                    if(offLineTaskList!=null && offLineTaskList.size()>0){
//                        pageIndex++;
//                    }
//                }
//            });
//        }
//

    }

}

